package org.spring.springboot.app.sys.menu.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.app.sys.menu.dao.SysMenuDao;
import org.spring.springboot.app.sys.menu.domain.po.SysMenuPO;
import org.spring.springboot.app.sys.menu.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysMenuServiceImpl implements ISysMenuService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysMenuServiceImpl.class);

    @Autowired
    private SysMenuDao sysMenuDao;

//    @Autowired
//    private RedisKeyUtils redisKeyUtils;


    @Override
    public List<SysMenuPO> findSysMenuByUserId(String userId) {
        return sysMenuDao.findSysMenuByUserId(userId);
    }

    @Override
    public List<SysMenuPO> findAll() {
        return sysMenuDao.findAll();
    }

    @Override
    public int save(SysMenuPO sysMenuPO) {
        if(sysMenuPO.getParentId()==null || sysMenuPO.getParentId() == ""){
            sysMenuPO.setParentId("1");
        }
        if(sysMenuPO.getSort()==null||sysMenuPO.getSort() < 1 ){
            sysMenuPO.setSort(sysMenuDao.findMaxSortByParentId(sysMenuPO.getParentId()));
        }
        sysMenuPO.setParentIds(sysMenuDao.findParentsByParentId(sysMenuPO.getParentId()));
        sysMenuPO.preInsert();
        return sysMenuDao.insertSelective(sysMenuPO);
    }

    @Override
    public int update(SysMenuPO sysMenuPO) {
        sysMenuPO.preUpdate();
        return sysMenuDao.updateByPrimaryKeySelective(sysMenuPO);
    }

    @Override
    public int delete(String id) {
        SysMenuPO sysMenuPO = new SysMenuPO();
        sysMenuPO.setId(id);
        sysMenuPO.preDelete();
        sysMenuDao.deleteChildren(sysMenuPO);
        return sysMenuDao.updateByPrimaryKeySelective(sysMenuPO);
    }


}
